<template>
	<view class="order-confirm">
		<view class="order-confirm-container">
			<Address></Address>
			<GoodsList :list="cartInfo"></GoodsList>
			<van-submit-bar
			  :price="totalPrice"
			  button-text="立即结算"
			  :disabled="disabledOrderConfirm"
			  @submit="onConfirmOrder"
			>
			</van-submit-bar>
		</view>
	</view>
</template>

<script>
	import { mapState, mapMutations } from 'vuex'
	import Address from './components/Address'
	import GoodsList from './components/GoodsList'
	import VanSubmitBar from '@/wxcomponents/vant/submit-bar/index'
	import {
		getOrderConfirmGoodsInfo as getOrderConfirmGoodsInfoApi,
		createOrder as createOrderApi
	} from '@/api/order'
	import {
		addressDefault as addressDefaultApi
	} from '@/api/address'
	export default {
		components: {
			Address,
			GoodsList,
			VanSubmitBar
		},
		data() {
			return {
				cartIds: '',
				cartInfo: [],
				orderKey: ''
			}
		},
		computed: {
			...mapState({
				currentAddress: state => state.user.currentAddress
			}),
			totalPrice () {
				let totalPrice = 0
				this.cartInfo.forEach((cart) => {
					totalPrice += cart.truePrice * 100 * cart.cart_num
				})
				return totalPrice
			},
			disabledOrderConfirm () {
				return !this.currentAddress || (this.cartInfo.length === 0)
			}
		},
		onLoad ({ cartIds }) {
			this.cartIds = cartIds

			this.getOrderConfirmGoodsInfo()
		},
		onShow () {
			this.getDefaultAddress()
		},
		methods: {
			...mapMutations({
				SET_CURRENT_ADDRESS: 'SET_CURRENT_ADDRESS'
			}),
			onConfirmOrder () {
				this.createOrder()
			},
			async getOrderConfirmGoodsInfo () {
				const params = {
					cartId: this.cartIds,
					new: 0
				}
				const { status, data, msg } = await getOrderConfirmGoodsInfoApi(params)
				if (status === this.API_STATUS_CODE.SUCCESS) {
					this.cartInfo = data.cartInfo
					this.orderKey = data.orderKey
				} else {
					uni.showToast({
						icon: 'none',
						title: msg,
						duration: 3000
					})
				}
			},
			async getDefaultAddress () {
				// 如果vuex state中存在用户收货地址，则不发起获取默认地址请求
				if (this.currentAddress) {
					return false
				}
				const { status, data, msg } = await addressDefaultApi()
				if (status === this.API_STATUS_CODE.SUCCESS) {
					if (data['id']) {
						this.SET_CURRENT_ADDRESS(data)
					}
				} else {
					uni.showToast({
						icon: 'none',
						title: msg,
						duration: 3000
					})
				}
			},
			async createOrder () {
				const params = {
					addressId: this.currentAddress.id,
					couponId: 0,
					payType: "weixin",
					mark: ""
				}
				const { status, data, msg } = await createOrderApi(this.orderKey, params)
				if (status === this.API_STATUS_CODE.SUCCESS) {
					uni.showToast({
						url: '订单创建成功，目前暂不可支付！！！'
					})
					uni.redirectTo({
						url: '/pages/order_detail/order_detail'
					})
				} else {
					uni.showToast({
						icon: 'none',
						title: msg,
						duration: 3000
					})
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
.order-confirm {
	&-container {
		address {
			width: 100%;
		}
		
		::v-deep van-submit-bar {
			.van-submit-bar__text {
				text-align: left;
			}
		}
	}
}
</style>
